/*
 * @Author: c
 * @Date: 2024-01-16 04:48:22
 * @LastEditTime: 2024-02-06 02:57:36
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\system\minio\entities\file_info.entity.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// Copyright 2024 Administrator
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Logger } from "@nestjs/common";
import { Album } from "src/core/music/album/entities/album.entity";
import { RateFileInfo } from "src/core/music/rate_file_info/entity/audio.enity";
import { Song } from "src/core/music/song/entities/song.entity";
import { Track } from "src/core/music/track/entities/track.entity";
import { AfterRemove, BeforeRemove, Column, CreateDateColumn, DeleteDateColumn, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import { FileFormatEnum, MimeEnum, StatusTypeEnum } from "../enum";



@Entity({
    name: "file_info"
})
export class FileInfo {

    constructor(id: string) {
        this.id = id
    }

    // @PrimaryColumn({
    //     name: "id",
    //     type: 'varchar',
    //     comment: "id: creator + uid + name"
    // })
    // id: string;

    @PrimaryGeneratedColumn("uuid")
    id: string;


    @Column({
        name: "creator",
        type: 'varchar',
        comment: "创建者的username"
    })
    creator: string;

    //文件名称
    @Column({
        name: "name",
        type: 'varchar',
        comment: "文件名称"
    })
    name: string;

    //浏览器文件选择读取文件时的时间戳
    @Column({
        name: "uid",
        type: "bigint",
        nullable: true,
        comment: "浏览器文件选择读取文件时的时间戳"
    })
    uid: number;


    //文件最后修改时间
    @Column({
        name: "last_modified",
        type: 'bigint',
        nullable: true,
        comment: "文件最后修改时间"
    })
    lastModified: number;


    //minio objectName名称
    @Column({
        name: "object_name",
        type: 'varchar',
        comment: "minio objectName名称"
    })
    objectName: string;

    //minio 存储桶名称
    @Column({
        name: "bucket",
        type: 'varchar',
        comment: "minio 存储桶名称"
    })
    bucket: string;

    //上传进度
    @Column({
        name: "percentage",
        type: "tinyint",
        comment: "上传进度",
        nullable: true,
        default: 0
    })
    percentage: number;

    //转码进度
    @Column({
        name: "transform_progress",
        type: "tinyint",
        comment: "转码进度",
        nullable: true,
        default: 0
    })
    transformProgress: number;

    //文件的状态
    @Column({
        name: "status",
        type: "enum",
        enum: StatusTypeEnum,
        nullable: true,
        comment: "文件的状态",
        default: StatusTypeEnum.READY
    })
    status: StatusTypeEnum;

    //文件的后缀
    @Column({
        name: "format",
        type: "enum",
        enum: FileFormatEnum,
        nullable: true,
        comment: "文件的后缀",
        default: FileFormatEnum.UNKOWN
    })
    format: FileFormatEnum;

    //文件大小
    @Column({
        name: "size",
        comment: "文件大小",
        nullable: true,
    })
    size: number;


    //文件的链接/本地的或者远程的
    @Column({
        name: "url",
        type: "varchar",
        comment: "文件的链、本地的或者远程的",
        default: ''
    })
    url: string;


    /**
     * https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
     */
    //文件mime类型
    @Column({
        name: "mime_type",
        type: 'enum',
        enum: MimeEnum,
        nullable: true,
        comment: "文件mime类型"
    })
    mimeType: MimeEnum;


    @Column({
        name: "deleted",
        type: "boolean",
        comment: "是否删除(false: 未删除 true: 删除)",
        default: 0
    })
    deleted: boolean;

    @CreateDateColumn({
        name: "created_date",
        type: "timestamp",
        default: () => "CURRENT_TIMESTAMP(6)"
    })
    createdDate: Date;

    @UpdateDateColumn({
        name: "updated_date",
        type: "timestamp",
        default: () => "CURRENT_TIMESTAMP(6)"
    })
    updatedDate: Date;

    @DeleteDateColumn({
        name: "deleted_time",
        nullable: false,
    })
    deletedTime: Date;

    @OneToOne(() => RateFileInfo, (rate) => rate.fileInfo, {
        // cascade: true,
        onDelete: 'CASCADE',
        // eager: true,
    })
    @JoinColumn({
        name: 'rate_id',
        referencedColumnName: "id",
    })
    rateFileInfo: RateFileInfo


    //关联到歌曲实体 多对一 关系
    @ManyToOne(() => Song, song => song.pic,
        {
            onDelete: 'CASCADE', //根据songId外键级联删除fileinfo
        }
    )
    @JoinColumn({
        name: 'song_id',
        referencedColumnName: "id",
    })
    song: Song;


    //关联到专辑实体 一对一 关系
    @OneToOne(() => Album, album => album.pic,
        {
            onDelete: 'CASCADE', //根据songId外键级联删除fileinfo
        }
    )
    @JoinColumn({
        name: 'album_id',
        referencedColumnName: "id",
    })
    album: Album;

    //关联到歌单实体 一对一 关系
    @OneToOne(() => Track, track => track.pic,
        {
            onDelete: 'CASCADE', //根据trackId外键级联删除fileinfo
        }
    )
    @JoinColumn({
        name: 'track_id',
        referencedColumnName: "id",
    })
    track: Track;


    @BeforeRemove()
    deleteMinioFile() {
        Logger.log("id" + this.id)
    }


    @AfterRemove()
    updateStatus() {
        Logger.log("id" + this.id)
    }


}